<template>
  <el-container class="layout-container-demo" style="height: 100vh">
    <el-aside :width="collapse ? '60px' : '200px'">
      <!-- 顶部 header -->
      <div class="aside-header">
        <!-- 头像 -->
        <el-avatar
            v-show="!collapse"
            size="32"
            src="src/assets/logo3.png"
        />
        <!-- 收起按钮 -->
        <el-tooltip
            :content="collapse ? '展开导航' : '收起导航'"
            placement="right"
            popper-class="my-nav-tooltip"
            :hide-after="0"
        >
          <el-avatar
              size="32"
              src="src/assets/collapse-btn.png"
              @click="collapse = !collapse"
              class="avatar-pointer"
          />
        </el-tooltip>
      </div>
      <el-scrollbar>
        <el-menu :default-active="$route.path"
                 router
                 :collapse="collapse"
                 :collapse-transition="false">
          <el-menu-item index="aiManage">
            <el-icon><DataLine /></el-icon>
            <span v-show="!collapse">AI超级管理</span>
          </el-menu-item>
          <el-menu-item index="userManage">
            <el-icon><User /></el-icon>
            <span v-show="!collapse">用户管理</span>
          </el-menu-item>
          <el-menu-item index="creation">
            <el-icon><Cpu /></el-icon>
            <span v-show="!collapse">AI创作</span>
          </el-menu-item>
          <!-- 新增：创作中心 -->
          <el-menu-item index="creationSquare">
            <el-icon><MagicStick /></el-icon>
            <span v-show="!collapse">创作广场</span>
          </el-menu-item>  <!-- 新增：创作中心 -->
          <el-menu-item index="aiSearch">
            <el-icon><Search /></el-icon>
            <span v-show="!collapse">创作广场AI搜索</span>
          </el-menu-item>
          <el-menu-item index="imageGen">
            <el-icon><Picture /></el-icon>
            <span v-show="!collapse">图片生成</span>
          </el-menu-item>
          <el-menu-item index="aiChat">
            <el-icon><ChatDotRound /></el-icon>
            <span v-show="!collapse">AI对话</span>
          </el-menu-item>
          <el-menu-item index="aiAnalysis">
            <el-icon><DataAnalysis /></el-icon>
            <span v-show="!collapse">AI解析</span>
          </el-menu-item>


          <el-menu-item index="creationlist">
            <el-icon><Document /></el-icon>
            <span v-show="!collapse">个人作品</span>
          </el-menu-item><el-menu-item index="chart">
            <el-icon><Document /></el-icon>
            <span v-show="!collapse">数据统计</span>
          </el-menu-item>
          <el-menu-item index="followList">
            <el-icon><Star /></el-icon>
            <span v-show="!collapse">我的关注</span>
          </el-menu-item>
        </el-menu>
      </el-scrollbar>
      <div class="user-info">
        <el-avatar
            size="32"
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            class="user-avatar"
        />
        <span v-show="!collapse" class="user-name">Admin</span>
      </div>

    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <div class="toolbar">
          <el-dropdown>
            <el-icon style="margin-right: 8px; margin-top: 1px">
              <setting />
            </el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出</el-dropdown-item>
                <el-dropdown-item @click="logout">注销</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main style="flex: 1; display: flex; flex-direction: column;">
        <!-- 其他布局内容 -->
        <router-view v-slot="{ Component }">
          <keep-alive :include="['AiSearch', 'CreationSquare', 'CreationList']">
            <component :is="Component" :key="$route.name" />
          </keep-alive>
        </router-view>

      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import {Menu as IconMenu, Message, Setting, Star} from '@element-plus/icons-vue'
import {showSuccessToast} from "vant";
import {ElMessageBox} from "element-plus";
import {useRouter} from "vue-router";

const collapse = ref(false)   // 折叠状态

const item = {
  date: '2016-05-02',
  name: 'Tom',
  address: 'No. 189, Grove St, Los Angeles',
}
const tableData = ref(Array.from({ length: 20 }).fill(item))
// 添加路由实例
const router = useRouter()

// 退出登录
const logout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 清除登录状态（根据实际项目需求调整）
    // localStorage.removeItem('token')

    // 跳转到登录页
    router.push('/login')

    showSuccessToast('已退出登录')
  }).catch(() => {
    // 用户取消操作
  })
}

// 注销账户
const destroyAccount = () => {
  ElMessageBox.confirm('确定要注销账户吗？此操作不可恢复！', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'error'
  }).then(() => {
    // 调用注销账户接口（根据实际项目需求调整）
    /*
    _axios.post('/user/destroy')
      .then(() => {
        ElMessage.success('账户已注销')
        router.push('/login')
      })
      .catch(() => {
        ElMessage.error('注销失败')
      })
    */
    // 暂时跳转到登录页
    router.push('/login')
    showSuccessToast('账户已注销')
  }).catch(() => {
    // 用户取消操作
  });
}
</script>

<style scoped>
/* 侧边栏按钮 */
/* 提示框本体 */
.avatar-pointer {
  --el-avatar-bg-color: transparent;   /* 或 #f3f5f6 与父级一致 */
  cursor: pointer;
}
/* 如果你还想悬停变色，可以加滤镜或透明度 */
.avatar-pointer:hover {
  opacity: 0.8;
}

/* 侧边栏顶部条 */
.aside-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px 0 16px;
  background: #f3f5f6;   /* 与 aside 背景一致 */
  border-bottom: 1px solid #e4e7ed;
}
/* 让菜单占满剩余高度 */
.el-aside {
  display: flex;
  flex-direction: column;
  background: #f3f5f6;
}
.user-avatar {
  margin-left: 10px;
  margin-bottom: 15px;
}
.user-info {
  display: flex;
  align-items: center;
  width: 100%;
}
.user-name {
  white-space: nowrap;   /* 不让文字换行 */
  padding-left:10px;
}
.el-aside .el-scrollbar {
  flex: 1;
}
.layout-container-demo .el-header {
  position: relative;
  background-color: #f3f5f6;
  color: var(--el-text-color-primary);
}
.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: #f3f5f6;
}
.el-menu-bg-color{
  background: #f3f5f6;
}
.layout-container-demo .el-menu {
  border-right: none;
}
.layout-container-demo .el-main {
  padding: 0;
}
.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}
.el-menu {
  --el-menu-bg-color: #f3f5f6;   /* 你想要的背景色 */
  --el-menu-hover-bg-color: #e9ecef;
  --el-menu-text-color: #303133;
  --el-menu-active-color: #409eff;
}
</style>